#!/usr/bin/env python
#coding:utf8

from BeautifulSoup import BeautifulSoup
from urllib2 import urlopen
import re
import os
import time
import multiprocessing
import threading
import memcache 

mc = memcache.Client(['127.0.0.1:12000'],debug=0) 
	
	
def mail(theme,subject):
	cmd = "./sendEmail -f qbcarl@163.com -t 429609918@qq.com -s smtp.163.com  -xu qbcarl -xp qb1234 -u '%s' -m '%s'"%(theme,subject)
	os.system(cmd)
	
	
class notice3(multiprocessing.Process):
	def __init__(self,flag,url):
		super(notice3,self).__init__()
		self.flag = flag
		self.url = url
	
	def run(self):
		flag = self.flag
		content = urlopen(self.url)
		soup = BeautifulSoup(content)
		wrap = soup.html.body.find('div',attrs = {'class':'wrap_datachart'})
		chart = wrap.findAll('tr', attrs={'class':"tbody_tr"}) 
		max = chart[-3]
		last = chart[-5]
		maxyilou = [int(v.renderContents()) for v in max.findAll("td")[7:11]]
		lastvalue = [int(v.renderContents()) for v in last.findAll("td")[11:15]]
		kaijiang = int(last.find('td', attrs={'class':re.compile('cbg2\d')}).renderContents())
		if kaijiang in lastvalue:
			lastvalue[lastvalue.index(kaijiang)]=0
	
		value = zip(lastvalue,maxyilou)
		print value
	
		for i,j in value:
			if i == 0:
				file = flag+"result"
				num = str([9,10,11,12][value.index((i,j))])
				rst = mc.get(file)
				if not rst:
					mc.set(file,num)
				else:
					if rst == num:
						continue
					else:
						mc.set(file,num)		
				#mail(file,num)
			
			if i >= j - 3 :
				num = str([9,10,11,12][value.index((i,j))])
				rst = mc.get(file)
				if not rst:
					mc.set(file,num)
				else:
					if rst == num:
						continue
					else:
						mc.set(file,num)		
				mail(flag,'number:%s,value(%s,%s)\n%s'%(num,i,j,str(value)))
	
	
class notice5(multiprocessing.Process):
	def __init__(self,flag,url):
		super(notice5,self).__init__()
		self.flag = flag
		self.url = url
	
	def run(self):	
		flag = self.flag
		content = urlopen(self.url)
		soup = BeautifulSoup(content)
		wrap = soup.html.body.find('div',attrs = {'class':'wrap_datachart'})
		chart = wrap.findAll('tr', attrs={'class':"tbody_tr"}) 
		max = chart[-2]
		last = chart[-5]
		maxyilou = [int(v.renderContents()) for v in max.findAll("td")[12:23]]
		lastvalue = [int(v.renderContents()) for v in last.findAll("td")[13:24]]
		firstnum = int(last.find('td', attrs={'class':re.compile('chartBall02')}).renderContents())
		lastvalue[lastvalue.index(firstnum)]=0
		value = zip(lastvalue,maxyilou)
		print value
		for i,j in value:
			if i == 0:
				file = flag+"result"
				num = str(range(1,12,1)[value.index((i,j))])
				rst = mc.get(file)
				if not rst:
					mc.set(file,num)
				else:
					if rst == num:
						continue
					else:
						mc.set(file,num)		
				#mail(file,num)

			if i >= j - 3:
				num = str(range(1,12,1)[value.index((i,j))])				
				rst = mc.get(file)
				if not rst:
					mc.set(file,num)
				else:
					if rst == num:
						continue
					else:
						mc.set(file,num)		
				mail(flag,'number:%s,value(%s,%s)\n%s'%(num,i,j,str(value)))	

class notice(multiprocessing.Process):
	def __init__(self,flag,url):
		super(notice,self).__init__()
		self.flag = flag
		self.url = url
	
	def run(self):
		flag = self.flag
		content = urlopen(self.url)
		soup = BeautifulSoup(content)
		table = soup.html.body.find('table',attrs = {'class':'table_box odd_even'})
		chart = table.findAll('tr', attrs={'onmouseover':"mouseover(this)"}) 
		keys = []
		res = []
		dis = 5
		if flag == "k3dz":
			dis = 10
		for item in chart:
			nums = item.findAll('td')
			if int(nums[6].renderContents()) >= int(nums[8].renderContents())-dis:
				keys.append((nums[0].find("div").renderContents().split('\r')[0],nums[6].renderContents(),nums[8].renderContents()))
			res.append((nums[0].find("div").renderContents().split('\r')[0],nums[6].renderContents(),nums[8].renderContents()))		
		print res

		if keys:
			rst = mc.get(flag)
			if not rst:
				mc.set(flag,rst)
				mail(flag,keys)
			else:
				if rst == str(keys):
					pass
				else:
					mc.set(flag,str(keys))
					mail(flag,keys)	
				
		
				
			
if __name__=="__main__":
	start = time.time()
	
	k3 = "http://888.qq.com/info/chart/k3/index.php?op=hzzs&days=10"
	jk3 = "http://888.qq.com/info/chart/jk3/index.php?op=hzzs&days=10"
	x5 = "http://888.qq.com/info/chart/dlc/index.php?op=q2fb&days=780"
	nx5 = "http://888.qq.com/info/chart/gdx/index.php?op=q2fb&days=840"
	ox5 = "http://888.qq.com/info/chart/syy/index.php?op=q2fb&days=780"

	k3dz = "http://zst.kuaicaile.com/static/om/jsk3/2thdx.html"
	k3sum = "http://zst.kuaicaile.com/static/om/jsk3/sum.html"
	x5q1 = "http://zst.kuaicaile.com/static/om/jx11x5/h1.html"	
	nx5q1 = "http://zst.kuaicaile.com/static/om/gd11x5/h1.html"
	

	plist = []
	plist.append(notice3("k3",k3))
	plist.append(notice3("newk3",jk3))
#	plist.append(notice5("5of11",x5))
#	plist.append(notice5("old5of11",ox5))
#	plist.append(notice5("new5of11",nx5))
	plist.append(notice("k3dz",k3dz))
	plist.append(notice("k3sum",k3sum))
	plist.append(notice("5of11first1",x5q1))
	plist.append(notice("new5of11first1",nx5q1))

	
	for p in plist:
		p.start()
	for p in plist:
		p.join()

		
	print time.time()-start







